package com.example.foo.product.service;

import java.time.LocalDateTime;
import java.util.List;

import com.example.core.PageResult;
import com.example.core.Pageable;
import com.example.foo.product.model.Bag;
import com.example.foo.product.query.BagQuery;
import com.example.foo.product.dao.BagDao;

@org.springframework.stereotype.Service
public class BagService {

	private final BagDao bagDao;

	public BagService(BagDao bagDao) {
		this.bagDao = bagDao;
	}

	// ========== Bag ==========

	public PageResult<Bag> findBags(BagQuery bagQuery, Pageable pageable) {
		return bagDao.findBags(bagQuery, pageable);
	}

	public List<Bag> findBagsByIds(List<Long> ids) {
		return bagDao.selectBatchIds(ids);
	}

	public Bag findBagById(Long id) {
		return bagDao.selectById(id);
	}

	public void createBag(Bag bag) {
		bag.setState(Bag.State.unpublished);
		bag.setCreatedAt(LocalDateTime.now());
		bagDao.insert(bag);
	}

	public void updateBag(Bag bag) {
		bag.setUpdatedAt(LocalDateTime.now());
		bagDao.updateById(bag);
	}

	public void updateBagState(Long id, Bag.State state) {
		Bag bag = new Bag();
		bag.setId(id);
		bag.setState(state);
		bag.setUpdatedAt(LocalDateTime.now());
		bagDao.updateById(bag);
	}

}
